import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'setting_controller.dart';

class SettingPage extends StatelessWidget {
  final controller = Get.put(SettingController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(
        context: context,
        title: "设置",
      ),
      body: Column(
        children: [
          SizedBox(
            height: 12.w,
          ),
          Material(
            child: InkWell(
              onTap: () {
                controller.checkNewVersion(true);
              },
              child: Padding(
                padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 16.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "当前版本",
                      style: TextStyle(
                        color: Colours.color_6f7082,
                        fontSize: 16.sp,
                      ),
                    ),
                    GetBuilder<SettingController>(
                      builder: (controller) {
                        return Row(
                          children: [
                            Visibility(
                              visible: controller.hasNewVersion,
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Colours.color_theme,
                                  borderRadius: BorderRadius.circular(8.w),
                                ),
                                padding: EdgeInsets.symmetric(vertical: 1.w, horizontal: 7.w),
                                child: Text(
                                  "新版本",
                                  style: TextStyle(color: Colours.color_white, fontSize: 10.sp),
                                ),
                              ),
                            ),
                            SizedBox(
                              width: 8.w,
                            ),
                            Text(
                              "V${controller.versionName}",
                              style: TextStyle(color: Colours.color_b7b8c1, fontSize: 12.sp),
                            ),
                            SizedBox(
                              width: 8.w,
                            ),
                            Image.asset("images/ic_arrow_right_gray.png"),
                          ],
                        );
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
          SizedBox(
            height: 1.w,
          ),
          Material(
            child: InkWell(
              onTap: () {
                Navigator.pushNamed(context, Routes.privacyPolicy);
              },
              child: Padding(
                padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 16.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "用户协议",
                      style: TextStyle(
                        color: Colours.color_6f7082,
                        fontSize: 16.sp,
                      ),
                    ),
                    Image.asset("images/ic_arrow_right_gray.png"),
                  ],
                ),
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(vertical: 32.w, horizontal: 15.w),
            child: TextButton(
              onPressed: () {
                showLogoutDialog(context);
              },
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colours.color_white),
                shape: MaterialStateProperty.all(
                  RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(28.w),
                    side: BorderSide(width: 1.w, color: Colours.color_theme),
                  ),
                ),
                minimumSize: MaterialStateProperty.all(Size(double.infinity, 48.w)),
              ),
              child: Text(
                "退出登录",
                style: TextStyle(
                  color: Colours.color_theme,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  void showLogoutDialog(BuildContext context) {
    showCupertinoDialog(
      barrierDismissible: true,
      context: context,
      builder: (c) {
        return CupertinoAlertDialog(
          content: Text(
            "确认退出登陆?",
            style: TextStyle(
              color: Colours.color_text_theme,
              fontSize: 14.sp,
            ),
          ),
          actions: [
            CupertinoDialogAction(
              child: Text(
                "取消",
                style: TextStyle(color: Colours.color_6f7082, fontSize: 14.sp),
              ),
              onPressed: () {
                //Navigator.pop(c);
                Get.back();
              },
            ),
            CupertinoDialogAction(
              child: Text(
                "确定",
                style: TextStyle(color: Colours.color_theme, fontSize: 14.sp),
              ),
              onPressed: () {
                //Navigator.pop(c);
                Get.back();
                controller.logout();
              },
            ),
          ],
        );
      },
    );
  }
}
